package cn.lg.soar.common.util.jackson;

import cn.lg.soar.common.util.desensitization.DesensitizationUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
 * 敏感数据序列化器
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public interface Sensitive {

    /**
     * 手机号码脱敏
     */
    class PhoneSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeString(
                    DesensitizationUtil.phone(o.toString())
            );
        }
    }

    /**
     * 邮箱脱敏
     */
    class EmailSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeString(
                    DesensitizationUtil.email(o.toString())
            );
        }
    }

    /**
     * 身份证号码脱敏
     */
    class IdCardSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeString(
                    DesensitizationUtil.idCard(o.toString())
            );
        }
    }

    /**
     * 银行卡脱敏
     */
    class BankcardSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeString(
                    DesensitizationUtil.bankcard(o.toString())
            );
        }
    }

    /**
     * 姓名脱敏
     */
    class RealNameSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeString(
                    DesensitizationUtil.realName(o.toString())
            );
        }
    }

}
